package com.ruoyi.exam.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.result.Result;
import com.ruoyi.exam.domain.ExamPaper;
import com.ruoyi.exam.service.IExamPaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 试卷表 前端控制器
 *
 * @author FSD
 * @since 2024-02-18
 */
@Api(tags = "试卷表")
@RestController
@RequestMapping("/api/system/exam-paper")
public class ExamPaperController {

    private String prefix = "/api/system/exam-paper";
    @Resource(name = "ExamPaperServiceImpl")
    private IExamPaperService examPaperService;


    @RequiresPermissions("system:examPaper:view")
    @GetMapping()
    public String examPaper()
    {
        return prefix + "/examPaper";
    }
    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增", notes = "新增")
    @ApiImplicitParam(name = "examPaper", value = "examPaper实体类",dataTypeClass = String.class)
    @RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
    public Result insert(@RequestBody ExamPaper examPaper) {
        return Result.ok(examPaperService.save(examPaper));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "修改")
    @ApiImplicitParam(name = "examPaper", value = "examPaper实体类",dataTypeClass = String.class)
    @RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
    public Result update(@RequestBody ExamPaper examPaper) {
        return Result.ok(examPaperService.updateById(examPaper));
    }

    /**
     * 根据id删除
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    @ApiImplicitParam(name = "id", value = "id",dataTypeClass = Integer.class)
    @RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
    public Result delete(@PathVariable Integer id) {
        return Result.ok(examPaperService.removeById(id));
    }


    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部", notes = "查询全部")
    @RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
    public Result findAll() {
        return Result.ok(examPaperService.list());
    }

    /**
     * 分页模糊查询
     */
    @GetMapping("/get/page")
    @ApiOperation(value = "分页模糊查询", notes = "分页模糊查询")
    @RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<ExamPaper> queryWrapper = new QueryWrapper<>();
        // 倒序查询
        queryWrapper.orderByDesc("id");
        return Result.ok(examPaperService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}

